<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('map_services', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 服务基本信息
            $table->string('name', 100);
            $table->string('code', 50)->unique();
            $table->string('provider', 50); // amap, baidu, tencent, google
            $table->text('description')->nullable();

            // API配置
            $table->string('api_key', 200);
            $table->string('api_secret', 200)->nullable();
            $table->string('base_url', 500)->nullable();
            $table->json('api_endpoints')->nullable();

            // 坐标系配置
            $table->string('coordinate_system', 20)->default('wgs84'); // wgs84, gcj02, bd09
            $table->boolean('support_coordinate_conversion')->default(false);

            // 服务能力
            $table->boolean('support_geocoding')->default(true); // 地址转坐标
            $table->boolean('support_reverse_geocoding')->default(true); // 坐标转地址
            $table->boolean('support_routing')->default(false); // 路径规划
            $table->boolean('support_static_maps')->default(false); // 静态地图
            $table->boolean('support_places')->default(false); // 地点搜索
            $table->boolean('support_directions')->default(false); // 导航

            // 使用限制
            $table->integer('daily_quota')->nullable();
            $table->integer('monthly_quota')->nullable();
            $table->integer('requests_per_second')->default(10);
            $table->integer('requests_per_minute')->default(600);

            // 使用统计
            $table->integer('daily_usage')->default(0);
            $table->integer('monthly_usage')->default(0);
            $table->datetime('last_request_at')->nullable();
            $table->decimal('avg_response_time', 6, 3)->nullable(); // 毫秒

            // 服务状态
            $table->boolean('is_active')->default(true);
            $table->boolean('is_default')->default(false);
            $table->string('status', 20)->default('active'); // active, inactive, error
            $table->text('error_message')->nullable();

            // 配置选项
            $table->json('config_options')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['provider']);
            $table->index(['is_active']);
            $table->index(['is_default']);
            $table->index(['status']);
            $table->index(['code']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('map_services');
    }
};
